package com.briup.smartlabs.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.briup.smartlabs.bean.SysUser;
import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.mapper.SysUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "管理员信息管理服务")
@RestController
public class UserController {
	@Autowired
	private SysUserMapper userMapper;

	@ApiOperation(value = "返回所有用户信息")
	@GetMapping("/user/all")
	public Response<List<SysUser>> findAll(){
		List<SysUser> users = userMapper.selectAll();
		return Response.ok(users);
	}
	/**
	 * 
	 * @param pageSize	每页显示的条数
	 * @param pageNum	显示第几页的数据
	 * @return
	 */
	@ApiOperation(value = "分页显示所有用户信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "当前页码"),
		@ApiImplicitParam(name = "pageSize",value = "每页显示的数据个数")
	})
	@GetMapping("/user/by/page")
	public Response<PageInfo<SysUser>> findByPage(
			@RequestParam(defaultValue = "5")int pageSize,
			@RequestParam(defaultValue = "1")int pageNum){
		//表明开始分页，会对接下来代码中遇到的第一条sql语句进行分页查询
		PageHelper.startPage(pageNum, pageSize, true); 
		List<SysUser> users = userMapper.selectAll();
		PageInfo<SysUser> pages = new PageInfo<>(users);
		return Response.ok(pages);
	}
	
	@ApiOperation(value = "按照id删除用户信息")
	@ApiImplicitParam(name = "id",value = "要删除的用户的userId")
	@DeleteMapping("/user/by/id")
	public Response<String> deleteById(Long id){
		if(id==null) {
			return Response.error("请输入要删除的用户id！");
		}
		userMapper.deleteByPrimaryKey(id);
		return Response.ok("删除成功！");
	}
	
	@ApiOperation(value = "保存或者更新用户信息",
			notes = "1. 如果参数不包含用户id(userid),保存！<br/>"
				 + "2. 如果参数包含用户id(userid),更新！")
	@PostMapping("/user")
	public Response<String> saveOrUpdate(SysUser user){
		if(user.getUserId()==null) {
			//默认，如果数据没有传递主键值，就代表该数据在数据库中不存在，执行保存操作。
			user.setStatus((byte)1); //默认所有新建的账户都是正常状态
			user.setCreateTime(new Date());	//账户的创建时间就是当前时间
			user.setCreateUserId(1L);	//创建user账户的用户id应该为当前登录的账户id
			userMapper.insert(user);
		}else {
			userMapper.updateByPrimaryKey(user);
		}
		return Response.ok("操作成功！");
	}
	
	@ApiOperation(value = "按照名字模糊查找用户，并分页显示")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name",value = "用户名"),
		@ApiImplicitParam(name = "pageNum",value = "当前页码",defaultValue = "1"),
		@ApiImplicitParam(name = "pageSize",value = "每页显示的个数",defaultValue = "5")
	})
	@GetMapping("/user/by/name")
	public Response<PageInfo<SysUser>> findByName(
			String name,
			@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "5")int pageSize){
		PageHelper.startPage(pageNum, pageSize, true);
		if(name!=null&&!"".equals(name)) {
			name = "%"+name+"%";
		}
		List<SysUser> users = userMapper.selectNameLike(name);
		PageInfo<SysUser> pages = new PageInfo<>(users);
		return Response.ok(pages);
	}
	
	@ApiOperation(value = "按照用户id(userId)批量删除用户信息！")
	@DeleteMapping("/user/by/batch")
	public Response<String> deleteByBath(Long[] ids){
		if(ids==null||ids.length<=0) {
			return Response.error("请至少选择一个要删除的用户！");
		}
		for(Long id:ids) {
			userMapper.deleteByPrimaryKey(id);
		}
		return Response.ok("删除成功！");
	}
	
}















